/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.knowledge.doc.server.impl;

import com.wallellen.core.auth.domain.LoginUser;
import com.wallellen.core.sql.query.DataQuery;
import com.wallellen.core.time.TimeTool;
import com.wallellen.knowledge.doc.dao.TopDaoInter;
import com.wallellen.knowledge.doc.domain.Top;
import com.wallellen.knowledge.doc.server.TopServiceInter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

/* *
 *功能：置顶文档服务层实现类
 *详细：
 *
 *版本：v0.1
 *@author <a href="mailto:wallellen@hotmail.com">WALLE</a>
 *日期：20150707114057
 *说明：
 */
@Service
public class TopServiceImpl implements TopServiceInter {
    @Resource
    private TopDaoInter farmtopDaoImpl;


    @Override
    @Transactional
    public Top insertFarmtopEntity(Top entity, LoginUser user) {
        entity.setCtime(TimeTool.getTimeDate14());
        entity.setCuser(user.getId());
        entity.setCusername(user.getName());
        entity.setPstate("1");
        return farmtopDaoImpl.insertEntity(entity);
    }

    @Override
    @Transactional
    public Top editFarmtopEntity(Top entity, LoginUser user) {
        Top entity2 = farmtopDaoImpl.getEntity(entity.getId());
        entity2.setSort(entity.getSort());
        entity2.setDocid(entity.getDocid());
        entity2.setPcontent(entity.getPcontent());
        farmtopDaoImpl.editEntity(entity2);
        return entity2;
    }

    @Override
    @Transactional
    public void deleteFarmtopEntity(String id, LoginUser user) {

        farmtopDaoImpl.deleteEntity(farmtopDaoImpl.getEntity(id));
    }

    @Override
    @Transactional
    public Top getFarmtopEntity(String id) {

        if (id == null) {
            return null;
        }
        return farmtopDaoImpl.getEntity(id);
    }

    @Override
    @Transactional
    public DataQuery createFarmtopSimpleQuery(DataQuery query) {
        DataQuery dbQuery = DataQuery
                .init(query,
                        "FARM_TOP TOP "
                                + "LEFT JOIN FARM_DOC A ON TOP.DOCID = A.ID and a.STATE='1'"
                                + "LEFT JOIN FARM_RF_DOCTYPE B ON B.DOCID =A.ID "
                                + "LEFT JOIN FARM_DOCTYPE C ON C.ID=B.TYPEID "
                                + "LEFT JOIN FARM_DOCGROUP D ON D.ID=A.DOCGROUPID "
                                + "LEFT JOIN FARM_DOCRUNINFO DOCRUNINFO ON A.RUNINFOID = DOCRUNINFO.ID",
                        "TOP.ID AS ID, TOP.SORT AS SORT, A.DOCDESCRIBE AS DOCDESCRIBE,A.WRITEPOP AS WRITEPOP,"
                                + "A.READPOP AS READPOP,A.TITLE AS TITLE,A.AUTHOR AS AUTHOR,A.PUBTIME AS PUBTIME,"
                                + "A.DOMTYPE AS DOMTYPE,A.SHORTTITLE AS SHORTTITLE,A.TAGKEY AS TAGKEY,A.STATE AS STATE,"
                                + "D.GROUPNAME AS GROUPNAME, DOCRUNINFO.VISITNUM AS VISITNUM, DOCRUNINFO.ANSWERINGNUM AS ANSWERINGNUM, "
                                + "A.IMGID AS IMGID, A.ID AS DOCID");
        return dbQuery;

    }


    @Override
    public DataQuery docTopChooseDocList(DataQuery query) {
        DataQuery dbQuery = DataQuery
                .init(query,
                        "FARM_DOC  A "
                                + "LEFT JOIN FARM_RF_DOCTYPE B ON B.DOCID =A.ID "
                                + "LEFT JOIN FARM_DOCTYPE C ON C.ID=B.TYPEID "
                                + "LEFT JOIN farm_docgroup d ON d.ID=a.DOCGROUPID",
                        "A.ID AS ID,A.DOCDESCRIBE as DOCDESCRIBE,A.WRITEPOP as WRITEPOP,A.READPOP as READPOP,A.TITLE AS TITLE,A.AUTHOR AS AUTHOR,A.PUBTIME AS PUBTIME,A.DOMTYPE AS DOMTYPE,A.SHORTTITLE AS SHORTTITLE,A.TAGKEY AS TAGKEY,A.STATE AS STATE,D.GROUPNAME AS GROUPNAME ");

        dbQuery.addSqlRule(" AND NOT EXISTS (SELECT 1 FROM FARM_TOP TOP_ WHERE A.ID = TOP_.DOCID) ");
        return dbQuery;

    }

}
